package org.ricks.common.conf;

import org.ricks.log.Logger;
import org.ricks.utils.CollUtil;

import java.util.*;

/**
 * @author chenwei
 * @Title:
 * @Package
 * @Description:
 * @date 2020/8/717:25
 */
public class ActiveConfLoader extends AbstractConfLoader {

    private List<LoadFileName> loadFileNameList = new ArrayList<>();
    private Map<LoadFileName, Object> activeConfMap = new TreeMap<>();
    private Map<String, Object> activeConfValues;

    private ActiveConfLoader() {
    }

    private static class ActiveConfLoaderInner {
        private static ActiveConfLoader activeConfLoader = new ActiveConfLoader();
    }

    public static ActiveConfLoader getLoader() {
        return ActiveConfLoaderInner.activeConfLoader;
    }

    public void loadResult() throws Exception {
        getActiveConfFiles();
        loadSource(activeConfMap, loadFileNameList);
        activeConfValues = mergeSource(activeConfMap);
    }

    public void updateResult() throws Exception {
        loadSource(activeConfMap, loadFileNameList);
        activeConfValues = mergeSource(activeConfMap);
    }

    public Map<String, Object> getSource() {
        return activeConfValues;
    }

    public Map<LoadFileName, Object> getConfMap() {
        return activeConfMap;
    }

    /**
     * 获取active配置源
     */
    @SuppressWarnings("unchecked")
    private void getActiveConfFiles() throws Exception {
        Map<String, Object> map = ConfLoader.getLoader().getSource();
        if (CollUtil.isEmpty(map)) {
            Logger.warn("need to get conf source first");
//            throw new Exception("need to get conf source first");
        }
        Map<String, Object> tmpMap = map;
        String[] active = Constant.CONF_ACTIVE.split("\\.");
        String confName = null;

        // access active conf file
        for (int i = 0; i < active.length; i++) {
            String act = active[i];
            if (!tmpMap.containsKey(act)) {
                break;
            }
            if (i < active.length - 1) {
                if (tmpMap.get(act) instanceof String)
                    break;
                tmpMap = (Map<String, Object>) tmpMap.get(act);
            }
            if (i == active.length - 1) {
                if (!(tmpMap.get(act) instanceof String))
                    break;
                confName = (String) tmpMap.get(act);
            }
        }

        if (Objects.nonNull(confName)) {
            loadFileNameList.addAll(Arrays.asList(
                    new LoadFileName("application-" + confName + ".properties", LoadProperties.getInstance()) ));
        }
    }
}

